#include "star.h"
#define STAR_SPEED 0.2f;

CStar::CStar() : CMovable()
{
	life = 0;
}

void CStar::Activate(float X, float Y)
{
	reality = 12;
	life = 1;
	pos_x = X;
	pos_y = Y-8;
	lowest = Y-16;
	movingWay = 1;
	width = 16;
	height = 16;
	v_x = 0;
	v_y = -0.04f;
	startY = Y;
	anim = 0;
	last_time = 0;
	isLoading = 1;
}

void CStar::Update(int t)
{
	pos_x += v_x*t;
	pos_y += v_y*t;

	if(isLoading == 1 && startY - pos_y >= height) 
	{
		isLoading = 0;
		v_x = STAR_SPEED;
	}
	if(v_x == 0) return;
	cTree->Check(pos_x,pos_y+2,width,1);

	if(cTree->lastRes == 0 && v_y < 0.5f)
		v_y += 0.05f;
	else if(cTree->lastRes > 0)
	{
		if(ArrayObj[cTree->res[0]]->reality < 3)
		{
			v_y = -2*STAR_SPEED;
			pos_y = ArrayObj[cTree->res[0]]->pos_y - ArrayObj[cTree->res[0]]->height;
		}
	}
	cTree->Check(pos_x+width-1,pos_y-height+5,1,height-10);
	if(cTree->lastRes > 0)
	{
		if(ArrayObj[cTree->res[0]]->reality < 10)
		{
			pos_x = ArrayObj[cTree->res[0]]->pos_x - width;
			v_x = -v_x;
			movingWay = 0;
		}
	}

	cTree->Check(pos_x+1,pos_y-height+5,1,height-10);
	if(cTree->lastRes > 0)
	{
		if(ArrayObj[cTree->res[0]]->reality < 10)
		{
			pos_x = ArrayObj[cTree->res[0]]->pos_x + ArrayObj[cTree->res[0]]->width;
			v_x = -v_x;
			movingWay = 1;
		}
	}
}

void CStar::RenderFrame(LPDIRECT3DDEVICE9 d3ddv, int t)
{
	DWORD now = GetTickCount();
	Update(t);
	if (now - last_time > 100)
	{
		anim=(anim+1) % 4;
		last_time = now;
	}
	SpriteHandlerIP->Begin(D3DXSPRITE_ALPHABLEND);
	ItemPic->Render(_BackBuffer,pos_x,pos_y,2,anim);
	SpriteHandlerIP->End();
}

void CStar::Touch(CObjects* obj)
{
	life = 0;
	obj->havingStar = 1;
}